package com.caroil.caroilservice.network;

import android.content.Context;
import com.caroil.caroilservice.bean.BaseBean;
import com.caroil.caroilservice.bean.LoginBean;
import com.caroil.caroilservice.bean.TokenBean;
import com.caroil.caroilservice.network.Utils.MyObserver;
import com.caroil.caroilservice.network.Utils.RetrofitUtils;
import com.caroil.caroilservice.network.Utils.RxHelper;
import java.util.HashMap;
import java.util.Map;
import io.reactivex.Observer;
import okhttp3.MediaType;
import okhttp3.RequestBody;


/**
 * 提交参数方式
 */
public class RequestUtils {
    public static final MediaType JSON = MediaType
            .parse("application/json; charset=utf-8");

    /**
     * 本机号码一键登录
     *
     * @param context
     * @param loginToken
     * @param observer
     */
    public static void Login(Context context, String loginToken, MyObserver<LoginBean> observer) {
        Map<String, String> map = new HashMap<>();
        map.put("loginToken", loginToken);
        String json = MyGson.getInstance().toJson(map);
        RequestBody body = RequestBody.create(JSON, json);
        RetrofitUtils.getApiUrl()
                .postLogin(body).compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    /**
     * 手机验证码登录-获取手机短信验证码
     *
     * @param context
     * @param phone
     * @param observer
     */
    public static void getPhoneCode(Context context, String phone, MyObserver<BaseBean> observer) {
        Map<String, String> map = new HashMap<>();
        map.put("phone", phone);
        String json = MyGson.getInstance().toJson(map);
        RequestBody body = RequestBody.create(JSON, json);
        RetrofitUtils.getApiUrl()
                .postPhoneCode(body).compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    /**
     * 手机号验证码登录
     *
     * @param context
     * @param phone
     * @param code
     * @param observer
     */
    public static void PhoneCodeLogin(Context context, String phone, String code, MyObserver<LoginBean> observer) {
        Map<String, String> map = new HashMap<>();
        map.put("phone", phone);
        map.put("msgId", "");
        map.put("code", code);
        String json = MyGson.getInstance().toJson(map);
        RequestBody body = RequestBody.create(JSON, json);
        RetrofitUtils.getApiUrl()
                .postPhoneCodeLogin(body).compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }

    /**
     * 获取微信登录的token
     *
     * @param context
     * @param url
     * @param observer
     */
    public static void getWxToken(Context context, String url, Observer<TokenBean> observer) {
        RetrofitUtils.getApiUrl()
                .getToken(url).compose(RxHelper.observableIO2Main(context))
                .subscribe(observer);
    }


}



